// Redirect handler generator
// Copyright (C) 2011-2012 Jani Kajala. Licensed under MIT-license.
// Dependencies: Node.js (only core modules)

/**
 * Returns function(ServerRequest,ServerResponse) which redirects to specified URL using specified statusCode.
 * @param url URL to redirect to
 * @param status Status code to set. Default: 302 Moved Temporarily. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
 * @returns function(ServerRequest,ServerResponse)
 */
function redirect( url, status ) {
	return function(req,res,next) {
		res.statusCode = (typeof status == 'undefined' ? 302 : status);
		res.setHeader( "Location", url );
		res.end();
		if (typeof next === 'function')
			next();
	};
}

module.exports = redirect;
